package no.uia.ikt.gateway;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Gateway1Frame extends JFrame implements Runnable, WindowListener {

	private final static String TITLE = "Gateway 1";
	private TextArea text = new TextArea();
	private Gateway1 gateway;

	public Gateway1Frame() {
		super(TITLE);
		gateway = new Gateway1();
		getContentPane().setLayout(new BorderLayout());
		addWindowListener(this);
		add(text);
		pack();
		setVisible(true);
	}

	public Gateway1 getTvDevice() {
		return gateway;
	}

	private Thread timerThread = null;

	public void run() {
		Thread thisThread = Thread.currentThread();

		while (timerThread == thisThread) {
			text.setText(text.getText() + "\n" + gateway.getMessage() + "\n");
			this.repaint();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
	}

	public void start() {
		gateway.start();
		timerThread = new Thread(this);
		timerThread.start();
	}

	public void stop() {
		gateway.stop();
		timerThread = null;
	}

	// //////////////////////////////////////////////
	// window
	// //////////////////////////////////////////////
	public void windowActivated(WindowEvent e) {
	}

	public void windowClosed(WindowEvent e) {
	}

	public void windowClosing(WindowEvent e) {
		stop();
		System.exit(0);
	}

	public void windowDeactivated(WindowEvent e) {
	}

	public void windowDeiconified(WindowEvent e) {
	}

	public void windowIconified(WindowEvent e) {
	}

	public void windowOpened(WindowEvent e) {
	}

	public static void main(String args[]) {
		Gateway1Frame gateway = new Gateway1Frame();
		gateway.start();
	}
}
